***This file imports the raw survey data and cleans it.

*** Import the raw data and combine the files
clear
insheet using "Study2_raw06102017.csv", comma
save "Study2_cleanB.dta", replace

clear
insheet using "Study2_raw05042017.csv", comma
save "Study2_cleanA.dta", replace

append using "Study2_cleanB.dta", force

***Reformat treatment variables
gen treat=.

replace treat=1 if htc==1
replace treat=2 if ht_con==1 
replace treat=3 if t_treatleran==1 
replace treat=4 if treatmor==1

label variable treat "Treatment Groups"
label define treat 1 "Control" 2 "Immigration" 3 "Smuggling" 4 "Values" 
label values treat treat

*** Rescale dependent variables

gen wall2 = .
replace wall2=3 if wall==4
replace wall2=1 if wall==3
replace wall2=2 if wall==2
replace wall2=4 if wall==1

gen border = (20-htborder)/4
generate immR = (imm-1)/4
generate legalR = (legal-1)/3
generate hawkeyeR = (hawkeye-1)/3
generate uacR = (uac-1)
generate wallR = (wall2-1)/3
generate concernR = (concern-1)/4
generate problemR = (problem-1)/4


***** Clean up Demographics *****

*** Party identification: Republicans are low end of the scale 

gen pid7 = .
replace pid7=1 if pid==1 & pid_strong==1
replace pid7=2 if pid==1 & pid_strong==2
replace pid7=3 if pid_lean==1
replace pid7=4 if pid_lean==3
replace pid7=5 if pid_lean==2
replace pid7=6 if pid==2 & pid_strong==2
replace pid7=7 if pid==2 & pid_strong==1


label variable pid7 "Seven Point Party Identification"
label define pid7 3 "Strong Republican" 2 "Weak Republican" 1 "Lean Republican" 0 "Independent" -1 "Lean Democrat" -2 "Weak Democrat" -3 "Strong Democrat"
label values pid7 pid7

generate pid7R = (pid7-1)/6
label variable pid7R "Party Id" 

drop age
generate age = 2017 - born
generate ageR = (age - 18)/88
label variable ageR "Age"

generate female = sex
replace female=0 if sex==3
label variable female "Female"

generate incomeR = (income -1)/13
label variable income "Income"

generate white = 0
replace white =1 if race==1
label variable white "White"

generate rel = rel_freq - 1
replace rel = 0 if (religion ==5 | religion == 8 | religion ==9)
generate relR = rel/5
label variable relR "Religious Frequency"


*** Generate treatment groups by party

tab treat, gen(treat2)
foreach y of varlist treat22-treat24{
	gen pid_`y' = pid7*`y'
	}

gen rep =1 if pid7<4
replace rep = 0 if pid7 >4

foreach y of varlist treat22-treat24{
	gen rep_`y' = rep*`y'
	}
	
drop if treat == .



***DROPPING UNUSED VARIABLES ***

drop startdate - t_htc_clickcount v31- v34 t_treatlearn_firstclick - t_treatlearn_clickcount t_treatmor_firstclick - t_treatmor_clickcount q119_firstclick - q119_clickcount
drop t_programs_firstclick - t_programs_clickcount q121_firstclick - q121_clickcount q132_firstclick - q132_clickcount q122_firstclick - man_reform 
drop q123_firstclick - t_gic_clickcount t_percent_firstclick - t_percent_clickcount t_meds_firstclick - t_meds_clickcount t_threat_firstclick - t_threat_clickcount 
drop crime_thre - comp q124_firstclick - q124_clickcount q125_firstclick - q125_clickcount q126_firstclick - q126_clickcount q127_firstclick - q128_clickcount 
drop t_screen_firstclick - t_screen_clickcount q129_firstclick - q129_clickcount ft_race_1 - newuser


*** Generating/Rescaling moderating variables

gen college = 1 if school >=5
replace college = 0 if school <5

gen trump = 1 if pres == 1
replace trump = 0 if pres !=1 & pres !=.
recode trump (.=0) if vote_2016 == 2


*** Generate Variable Labels
set more off
label var treat22 "Immigration"
label var treat23 "Smuggling"
label var treat24 "Values"
label var ageR "Age"
label var trump "Voted For Trump"
label var female "Female"
label var incomeR "Income"
label var white "White"
label var rel "Religiosity"
label var college "College Degree"

gen immig_index = (immR+(1-wallR)+uacR+hawkeyeR+legalR)/5
gen wallR2 = 1-wallR

label var immR "Immigration Rate"
label var wallR2 "Mexican Border Wall"
label var uacR "Unaccompanied Children"
label var legalR "Path to Citizenship"
label var hawkeyeR "Illegal Immigration Policy"
label var immig_index "Immigration Attitudes Index"


*** Generate Economic Threat & Cultural Threat
gen econ_threa2 = (7-econ_threa)/6
gen cultl_thre_v2 = cultl_thre
replace cultl_thre_v2 = 2 if cultl_thre == 1
gen cultl_thre2_v2 = (8-cultl_thre_v2)/6

label var econ_threa2 "Economic Threat"
label var cultl_thre2_v2 "Cultural Threat"

*** Generate Ingroup-Centric Beliefs 
gen outgroupr = (7-outgroup)/6
gen ingroupr = (7-ingroup)/6
gen victimr = (7-victim)/6

label var outgroupr "Outgroup"
label var ingroupr "Ingroup"
label var victimr "Victim"



save "Study2_clean.dta", replace

